<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Working with forms &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Using Django" href="../index.html" />
    <link rel="next" title="Formsets" href="formsets.html" />
    <link rel="prev" title="How to use sessions" href="../http/sessions.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="../http/sessions.html" title="How to use sessions">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="formsets.html" title="Formsets">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-forms-index">
            
  <div class="section" id="s-working-with-forms">
<span id="working-with-forms"></span><h1>Working with forms<a class="headerlink" href="#working-with-forms" title="Permalink to this headline">¶</a></h1>
<div class="admonition-about-this-document admonition">
<p class="first admonition-title">About this document</p>
<p class="last">This document provides an introduction to the basics of web forms and how
they are handled in Django. For a more detailed look at specific areas of
the forms API, see <a class="reference internal" href="../../ref/forms/api.html"><em>The Forms API</em></a>, <a class="reference internal" href="../../ref/forms/fields.html"><em>Form fields</em></a>, and
<a class="reference internal" href="../../ref/forms/validation.html"><em>Form and field validation</em></a>.</p>
</div>
<p>Unless you&#8217;re planning to build Web sites and applications that do nothing but
publish content, and don&#8217;t accept input from your visitors, you&#8217;re going to
need to understand and use forms.</p>
<p>Django provides a range of tools and libraries to help you build forms to
accept input from site visitors, and then process and respond to the input.</p>
<div class="section" id="s-html-forms">
<span id="html-forms"></span><h2>HTML forms<a class="headerlink" href="#html-forms" title="Permalink to this headline">¶</a></h2>
<p>In HTML, a form is a collection of elements inside <tt class="docutils literal"><span class="pre">&lt;form&gt;...&lt;/form&gt;</span></tt> that
allow a visitor to do things like enter text, select options, manipulate
objects or controls, and so on, and then send that information back to the
server.</p>
<p>Some of these form interface elements - text input or checkboxes - are fairly
simple and built-in to HTML itself. Others are much more complex; an interface
that pops up a date picker or allows you to move a slider or manipulate
controls will typically use JavaScript and CSS as well as HTML form <tt class="docutils literal"><span class="pre">&lt;input&gt;</span></tt>
elements to achieve these effects.</p>
<p>As well as its <tt class="docutils literal"><span class="pre">&lt;input&gt;</span></tt> elements, a form must specify two things:</p>
<ul class="simple">
<li><em>where</em>: the URL to which the data corresponding to the user&#8217;s input should
be returned</li>
<li><em>how</em>: the HTTP method the data should be returned by</li>
</ul>
<p>As an example, the login form for the Django admin contains several
<tt class="docutils literal"><span class="pre">&lt;input&gt;</span></tt> elements: one of <tt class="docutils literal"><span class="pre">type=&quot;text&quot;</span></tt> for the username, one of
<tt class="docutils literal"><span class="pre">type=&quot;password&quot;</span></tt> for the password, and one of <tt class="docutils literal"><span class="pre">type=&quot;submit&quot;</span></tt> for the
&#8220;Log in&#8221; button. It also contains some hidden text fields that the user
doesn&#8217;t see, which Django uses to determine what to do next.</p>
<p>It also tells the browser that the form data should be sent to the URL
specified in the <tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt>’s <tt class="docutils literal"><span class="pre">action</span></tt> attribute - <tt class="docutils literal"><span class="pre">/admin/</span></tt> - and that it
should be sent using the HTTP mechanism specified by the <tt class="docutils literal"><span class="pre">method</span></tt> attribute -
<tt class="docutils literal"><span class="pre">post</span></tt>.</p>
<p>When the <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;submit&quot;</span> <span class="pre">value=&quot;Log</span> <span class="pre">in&quot;&gt;</span></tt> element is triggered, the
data is returned to <tt class="docutils literal"><span class="pre">/admin/</span></tt>.</p>
<div class="section" id="s-get-and-post">
<span id="get-and-post"></span><h3><tt class="docutils literal"><span class="pre">GET</span></tt> and <tt class="docutils literal"><span class="pre">POST</span></tt><a class="headerlink" href="#get-and-post" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">GET</span></tt> and <tt class="docutils literal"><span class="pre">POST</span></tt> are the only HTTP methods to use when dealing with forms.</p>
<p>Django&#8217;s login form is returned using the <tt class="docutils literal"><span class="pre">POST</span></tt> method, in which the browser
bundles up the form data, encodes it for transmission, sends it back to the
server, and then receives its response.</p>
<p><tt class="docutils literal"><span class="pre">GET</span></tt>, by contrast, bundles the submitted data into a string, and uses this
to compose a URL. The URL contains the address where the data must be sent, as
well as the data keys and values. You can see this in action if you do a search
in the Django documentation, which will produce a URL of the form
<tt class="docutils literal"><span class="pre">https://docs.djangoproject.com/search/?q=forms&amp;release=1</span></tt>.</p>
<p><tt class="docutils literal"><span class="pre">GET</span></tt> and <tt class="docutils literal"><span class="pre">POST</span></tt> are typically used for different purposes.</p>
<p>Any request that could be used to change the state of the system - for example,
a request that makes changes in the database - should use <tt class="docutils literal"><span class="pre">POST</span></tt>. <tt class="docutils literal"><span class="pre">GET</span></tt>
should be used only for requests that do not affect the state of the system.</p>
<p><tt class="docutils literal"><span class="pre">GET</span></tt> would also be unsuitable for a password form, because the password
would appear in the URL, and thus, also in browser history and server logs,
all in plain text. Neither would it be suitable for large quantities of data,
or for binary data, such as an image. A Web application that uses <tt class="docutils literal"><span class="pre">GET</span></tt>
requests for admin forms is a security risk: it can be easy for an attacker to
mimic a form&#8217;s request to gain access to sensitive parts of the system.
<tt class="docutils literal"><span class="pre">POST</span></tt>, coupled with other protections like Django&#8217;s <a class="reference internal" href="../../ref/contrib/csrf.html"><em>CSRF protection</em></a> offers more control over access.</p>
<p>On the other hand, <tt class="docutils literal"><span class="pre">GET</span></tt> is suitable for things like a web search form,
because the URLs that represent a <tt class="docutils literal"><span class="pre">GET</span></tt> request can easily be bookmarked,
shared, or resubmitted.</p>
</div>
</div>
<div class="section" id="s-django-s-role-in-forms">
<span id="django-s-role-in-forms"></span><h2>Django&#8217;s role in forms<a class="headerlink" href="#django-s-role-in-forms" title="Permalink to this headline">¶</a></h2>
<p>Handling forms is a complex business. Consider Django&#8217;s admin, where numerous
items of data of several different types may need to be prepared for display in
a form, rendered as HTML, edited using a convenient interface, returned to the
server, validated and cleaned up, and then saved or passed on for further
processing.</p>
<p>Django&#8217;s form functionality can simplify and automate vast portions of this
work, and can also do it more securely than most programmers would be able to
do in code they wrote themselves.</p>
<p>Django handles three distinct parts of the work involved in forms:</p>
<ul class="simple">
<li>preparing and restructuring data ready for rendering</li>
<li>creating HTML forms for the data</li>
<li>receiving and processing submitted forms and data from the client</li>
</ul>
<p>It is <em>possible</em> to write code that does all of this manually, but Django can
take care of it all for you.</p>
</div>
<div class="section" id="s-forms-in-django">
<span id="forms-in-django"></span><h2>Forms in Django<a class="headerlink" href="#forms-in-django" title="Permalink to this headline">¶</a></h2>
<p>We&#8217;ve described HTML forms briefly, but an HTML <tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt> is just one part of
the machinery required.</p>
<p>In the context of a Web application, &#8216;form&#8217; might refer to that HTML
<tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt>, or to the Django <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> that produces it, or to the
structured data returned when it is submitted, or to the end-to-end working
collection of these parts.</p>
<div class="section" id="s-the-django-form-class">
<span id="the-django-form-class"></span><h3>The Django <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> class<a class="headerlink" href="#the-django-form-class" title="Permalink to this headline">¶</a></h3>
<p>At the heart of this system of components is Django&#8217;s <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> class. In
much the same way that a Django model describes the logical structure of an
object, its behavior, and the way its parts are represented to us, a
<a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> class describes a form and determines how it works and appears.</p>
<p>In a similar way that a model class&#8217;s fields map to database fields, a form
class&#8217;s fields map to HTML form <tt class="docutils literal"><span class="pre">&lt;input&gt;</span></tt> elements. (A <a class="reference internal" href="modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><tt class="xref py py-class docutils literal"><span class="pre">ModelForm</span></tt></a>
maps a model class&#8217;s fields to HTML form <tt class="docutils literal"><span class="pre">&lt;input&gt;</span></tt> elements via a
<a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a>; this is what the Django admin is based upon.)</p>
<p>A form&#8217;s fields are themselves classes; they manage form data and perform
validation when a form is submitted. A <a class="reference internal" href="../../ref/forms/fields.html#django.forms.DateField" title="django.forms.DateField"><tt class="xref py py-class docutils literal"><span class="pre">DateField</span></tt></a> and a
<a class="reference internal" href="../../ref/forms/fields.html#django.forms.FileField" title="django.forms.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> handle very different kinds of data and have to do
different things with it.</p>
<p>A form field is represented to a user in the browser as a HTML &#8220;widget&#8221; - a
piece of user interface machinery. Each field type has an appropriate default
<a class="reference internal" href="../../ref/forms/widgets.html"><em>Widget class</em></a>, but these can be overridden as
required.</p>
</div>
<div class="section" id="s-instantiating-processing-and-rendering-forms">
<span id="instantiating-processing-and-rendering-forms"></span><h3>Instantiating, processing, and rendering forms<a class="headerlink" href="#instantiating-processing-and-rendering-forms" title="Permalink to this headline">¶</a></h3>
<p>When rendering an object in Django, we generally:</p>
<ol class="arabic simple">
<li>get hold of it in the view (fetch it from the database, for example)</li>
<li>pass it to the template context</li>
<li>expand it to HTML markup using template variables</li>
</ol>
<p>Rendering a form in a template involves nearly the same work as rendering any
other kind of object, but there are some key differences.</p>
<p>In the case of a model instance that contained no data, it would rarely if ever
be useful to do anything with one in a template. On the other hand, it makes
perfect sense to render an unpopulated form - that&#8217;s what we do when we want
the user to populate it.</p>
<p>So when we handle a model instance in a view, we typically retrieve it from the
database. When we&#8217;re dealing with a form we typically instantiate it in the
view.</p>
<p>When we instantiate a form, we can opt to leave it empty or pre-populate it, for
example with:</p>
<ul class="simple">
<li>data from a saved model instance (as in the case of admin forms for editing)</li>
<li>data that we have collated from other sources</li>
<li>data received from a previous HTML form submission</li>
</ul>
<p>The last of these cases is the most interesting, because it&#8217;s what makes it
possible for users not just to read a Web site, but to send information back
to it too.</p>
</div>
</div>
<div class="section" id="s-building-a-form">
<span id="building-a-form"></span><h2>Building a form<a class="headerlink" href="#building-a-form" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-the-work-that-needs-to-be-done">
<span id="the-work-that-needs-to-be-done"></span><h3>The work that needs to be done<a class="headerlink" href="#the-work-that-needs-to-be-done" title="Permalink to this headline">¶</a></h3>
<p>Suppose you want to create a simple form on your Web site, in order to obtain
the user&#8217;s name. You&#8217;d need something like this in your template:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;form</span> <span class="na">action=</span><span class="s">&quot;/your-name/&quot;</span> <span class="na">method=</span><span class="s">&quot;post&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">&quot;your_name&quot;</span><span class="nt">&gt;</span>Your name: <span class="nt">&lt;/label&gt;</span>
    <span class="nt">&lt;input</span> <span class="na">id=</span><span class="s">&quot;your_name&quot;</span> <span class="na">type=</span><span class="s">&quot;text&quot;</span> <span class="na">name=</span><span class="s">&quot;your_name&quot;</span> <span class="na">value=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">current_name</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">&quot;submit&quot;</span> <span class="na">value=</span><span class="s">&quot;OK&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;/form&gt;</span>
</pre></div>
</div>
<p>This tells the browser to return the form data to the URL <tt class="docutils literal"><span class="pre">/your-name/</span></tt>, using
the <tt class="docutils literal"><span class="pre">POST</span></tt> method. It will display a text field, labeled &#8220;Your name:&#8221;, and a
button marked &#8220;OK&#8221;. If the template context contains a <tt class="docutils literal"><span class="pre">current_name</span></tt>
variable, that will be used to pre-fill the <tt class="docutils literal"><span class="pre">your_name</span></tt> field.</p>
<p>You&#8217;ll need a view that renders the template containing the HTML form, and
that can supply the <tt class="docutils literal"><span class="pre">current_name</span></tt> field as appropriate.</p>
<p>When the form is submitted, the <tt class="docutils literal"><span class="pre">POST</span></tt> request which is sent to the server
will contain the form data.</p>
<p>Now you&#8217;ll also need a view corresponding to that <tt class="docutils literal"><span class="pre">/your-name/</span></tt> URL which will
find the appropriate key/value pairs in the request, and then process them.</p>
<p>This is a very simple form. In practice, a form might contain dozens or
hundreds of fields, many of which might need to be pre-populated, and we might
expect the user to work through the edit-submit cycle several times before
concluding the operation.</p>
<p>We might require some validation to occur in the browser, even before the form
is submitted; we might want to use much more complex fields, that allow the
user to do things like pick dates from a calendar and so on.</p>
<p>At this point it&#8217;s much easier to get Django to do most of this work for us.</p>
</div>
<div class="section" id="s-building-a-form-in-django">
<span id="building-a-form-in-django"></span><h3>Building a form in Django<a class="headerlink" href="#building-a-form-in-django" title="Permalink to this headline">¶</a></h3>
<div class="section" id="s-the-form-class">
<span id="the-form-class"></span><h4>The <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> class<a class="headerlink" href="#the-form-class" title="Permalink to this headline">¶</a></h4>
<p>We already know what we want our HTML form to look like. Our starting point for
it in Django is this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">NameForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="n">your_name</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="s">&#39;Your name&#39;</span><span class="p">,</span> <span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>This defines a <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> class with a single field (<tt class="docutils literal"><span class="pre">your_name</span></tt>). We&#8217;ve
applied a human-friendly label to the field, which will appear in the
<tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> when it&#8217;s rendered (although in this case, the <a class="reference internal" href="../../ref/forms/fields.html#django.forms.Field.label" title="django.forms.Field.label"><tt class="xref py py-attr docutils literal"><span class="pre">label</span></tt></a>
we specified is actually the same one that would be generated automatically if
we had omitted it).</p>
<p>The field&#8217;s maximum allowable length is defined by
<a class="reference internal" href="../../ref/forms/fields.html#django.forms.CharField.max_length" title="django.forms.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a>. This does two things. It puts a
<tt class="docutils literal"><span class="pre">maxlength=&quot;100&quot;</span></tt> on the HTML <tt class="docutils literal"><span class="pre">&lt;input&gt;</span></tt> (so the browser should prevent the
user from entering more than that number of characters in the first place). It
also means that when Django receives the form back from the browser, it will
validate the length of the data.</p>
<p>A <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> instance has an <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form.is_valid" title="django.forms.Form.is_valid"><tt class="xref py py-meth docutils literal"><span class="pre">is_valid()</span></tt></a> method, which runs
validation routines for all its fields. When this method is called, if all
fields contain valid data, it will:</p>
<ul class="simple">
<li>return <tt class="docutils literal"><span class="pre">True</span></tt></li>
<li>place the form&#8217;s data in its <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form.cleaned_data" title="django.forms.Form.cleaned_data"><tt class="xref py py-attr docutils literal"><span class="pre">cleaned_data</span></tt></a> attribute.</li>
</ul>
<p>The whole form, when rendered for the first time, will look like:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">&quot;your_name&quot;</span><span class="nt">&gt;</span>Your name: <span class="nt">&lt;/label&gt;</span>
<span class="nt">&lt;input</span> <span class="na">id=</span><span class="s">&quot;your_name&quot;</span> <span class="na">type=</span><span class="s">&quot;text&quot;</span> <span class="na">name=</span><span class="s">&quot;your_name&quot;</span> <span class="na">maxlength=</span><span class="s">&quot;100&quot;</span><span class="nt">&gt;</span>
</pre></div>
</div>
<p>Note that it <strong>does not</strong> include the <tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt> tags, or a submit button.
We&#8217;ll have to provide those ourselves in the template.</p>
</div>
<div class="section" id="s-the-view">
<span id="s-using-a-form-in-a-view"></span><span id="the-view"></span><span id="using-a-form-in-a-view"></span><h4>The view<a class="headerlink" href="#the-view" title="Permalink to this headline">¶</a></h4>
<p>Form data sent back to a Django Web site is processed by a view, generally the
same view which published the form. This allows us to reuse some of the same
logic.</p>
<p>To handle the form we need to instantiate it in the view for the URL where we
want it to be published:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponseRedirect</span>

<span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c"># if this is a POST request we need to process the form data</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&#39;POST&#39;</span><span class="p">:</span>
        <span class="c"># create a form instance and populate it with data from the request:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">NameForm</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
        <span class="c"># check whether it&#39;s valid:</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="c"># process the data in form.cleaned_data as required</span>
            <span class="c"># ...</span>
            <span class="c"># redirect to a new URL:</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s">&#39;/thanks/&#39;</span><span class="p">)</span>

    <span class="c"># if a GET (or any other method) we&#39;ll create a blank form</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">NameForm</span><span class="p">()</span>

    <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s">&#39;name.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">})</span>
</pre></div>
</div>
<p>If we arrive at this view with a <tt class="docutils literal"><span class="pre">GET</span></tt> request, it will create an empty form
instance and place it in the template context to be rendered. This is what we
can expect to happen the first time we visit the URL.</p>
<p>If the form is submitted using a <tt class="docutils literal"><span class="pre">POST</span></tt> request, the view will once again
create a form instance and populate it with data from the request: <tt class="docutils literal"><span class="pre">form</span> <span class="pre">=</span>
<span class="pre">NameForm(request.POST)</span></tt> This is called &#8220;binding data to the form&#8221; (it is now
a <em>bound</em> form).</p>
<p>We call the form&#8217;s <tt class="docutils literal"><span class="pre">is_valid()</span></tt> method; if it&#8217;s not <tt class="docutils literal"><span class="pre">True</span></tt>, we go back to
the template with the form. This time the form is no longer empty (<em>unbound</em>)
so the HTML form will be populated with the data previously submitted, where it
can be edited and corrected as required.</p>
<p>If <tt class="docutils literal"><span class="pre">is_valid()</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, we&#8217;ll now be able to find all the validated form
data in its <tt class="docutils literal"><span class="pre">cleaned_data</span></tt> attribute. We can use this data to update the
database or do other processing before sending an HTTP redirect to the browser
telling it where to go next.</p>
</div>
<div class="section" id="s-the-template">
<span id="s-topics-forms-index-basic-form-template"></span><span id="the-template"></span><span id="topics-forms-index-basic-form-template"></span><h4>The template<a class="headerlink" href="#the-template" title="Permalink to this headline">¶</a></h4>
<p>We don&#8217;t need to do much in our <tt class="docutils literal"><span class="pre">name.html</span></tt> template. The simplest example
is:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;form</span> <span class="na">action=</span><span class="s">&quot;/your-name/&quot;</span> <span class="na">method=</span><span class="s">&quot;post&quot;</span><span class="nt">&gt;</span>
    <span class="cp">{%</span> <span class="k">csrf_token</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">form</span> <span class="cp">}}</span>
    <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">&quot;submit&quot;</span> <span class="na">value=</span><span class="s">&quot;Submit&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/form&gt;</span>
</pre></div>
</div>
<p>All the form&#8217;s fields and their attributes will be unpacked into HTML markup
from that <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form</span> <span class="pre">}}</span></tt> by Django&#8217;s template language.</p>
<div class="admonition-forms-and-cross-site-request-forgery-protection admonition">
<p class="first admonition-title">Forms and Cross Site Request Forgery protection</p>
<p class="last">Django ships with an easy-to-use <a class="reference internal" href="../../ref/contrib/csrf.html"><em>protection against Cross Site Request
Forgeries</em></a>. When submitting a form via <tt class="docutils literal"><span class="pre">POST</span></tt> with
CSRF protection enabled you must use the <a class="reference internal" href="../../ref/templates/builtins.html#std:templatetag-csrf_token"><tt class="xref std std-ttag docutils literal"><span class="pre">csrf_token</span></tt></a> template tag
as in the preceding example. However, since CSRF protection is not
directly tied to forms in templates, this tag is omitted from the
following examples in this document.</p>
</div>
<div class="admonition-html5-input-types-and-browser-validation admonition">
<p class="first admonition-title">HTML5 input types and browser validation</p>
<p class="last">If your form includes a <a class="reference internal" href="../../ref/forms/fields.html#django.forms.URLField" title="django.forms.URLField"><tt class="xref py py-class docutils literal"><span class="pre">URLField</span></tt></a>, an
<a class="reference internal" href="../../ref/forms/fields.html#django.forms.EmailField" title="django.forms.EmailField"><tt class="xref py py-class docutils literal"><span class="pre">EmailField</span></tt></a> or any integer field type, Django will
use the <tt class="docutils literal"><span class="pre">url</span></tt>, <tt class="docutils literal"><span class="pre">email</span></tt> and <tt class="docutils literal"><span class="pre">number</span></tt> HTML5 input types. By default,
browsers may apply their own validation on these fields, which may be
stricter than Django&#8217;s validation. If you would like to disable this
behavior, set the <cite>novalidate</cite> attribute on the <tt class="docutils literal"><span class="pre">form</span></tt> tag, or specify
a different widget on the field, like <a class="reference internal" href="../../ref/forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
</div>
<p>We now have a working web form, described by a Django <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a>, processed
by a view, and rendered as an HTML <tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt>.</p>
<p>That&#8217;s all you need to get started, but the forms framework puts a lot more at
your fingertips. Once you understand the basics of the process described above,
you should be aware of what else is readily available in the forms system
and know a little bit about some of the underlying machinery.</p>
</div>
</div>
</div>
<div class="section" id="s-more-about-django-form-classes">
<span id="more-about-django-form-classes"></span><h2>More about Django <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> classes<a class="headerlink" href="#more-about-django-form-classes" title="Permalink to this headline">¶</a></h2>
<p>All form classes are created as subclasses of <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">django.forms.Form</span></tt></a>,
including the <a class="reference internal" href="modelforms.html"><em>ModelForm</em></a>, which you encounter
in Django&#8217;s admin.</p>
<div class="admonition-models-and-forms admonition">
<p class="first admonition-title">Models and Forms</p>
<p class="last">In fact if your form is going to be used to directly add or edit a Django
model, a <a class="reference internal" href="modelforms.html"><em>ModelForm</em></a> can save you a great
deal of time, effort, and code, because it will build a form, along with the
appropriate fields and their attributes, from a <tt class="docutils literal"><span class="pre">Model</span></tt> class.</p>
</div>
<div class="section" id="s-bound-and-unbound-form-instances">
<span id="bound-and-unbound-form-instances"></span><h3>Bound and unbound form instances<a class="headerlink" href="#bound-and-unbound-form-instances" title="Permalink to this headline">¶</a></h3>
<p>The distinction between <a class="reference internal" href="../../ref/forms/api.html#ref-forms-api-bound-unbound"><em>Bound and unbound forms</em></a> is important:</p>
<ul class="simple">
<li>An unbound form has no data associated with it. When rendered to the user,
it will be empty or will contain default values.</li>
<li>A bound form has submitted data, and hence can be used to tell if that data
is valid. If an invalid bound form is rendered, it can include inline error
messages telling the user what data to correct.</li>
</ul>
<p>The form&#8217;s <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form.is_bound" title="django.forms.Form.is_bound"><tt class="xref py py-attr docutils literal"><span class="pre">is_bound</span></tt></a> attribute will tell you whether a form has
data bound to it or not.</p>
</div>
<div class="section" id="s-more-on-fields">
<span id="more-on-fields"></span><h3>More on fields<a class="headerlink" href="#more-on-fields" title="Permalink to this headline">¶</a></h3>
<p>Consider a more useful form than our minimal example above, which we could use
to implement &#8220;contact me&#8221; functionality on a personal Web site:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">ContactForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="n">subject</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">Textarea</span><span class="p">)</span>
    <span class="n">sender</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">EmailField</span><span class="p">()</span>
    <span class="n">cc_myself</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<p>Our earlier form used a single field, <tt class="docutils literal"><span class="pre">your_name</span></tt>, a <a class="reference internal" href="../../ref/forms/fields.html#django.forms.CharField" title="django.forms.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a>. In
this case, our form has four fields: <tt class="docutils literal"><span class="pre">subject</span></tt>, <tt class="docutils literal"><span class="pre">message</span></tt>, <tt class="docutils literal"><span class="pre">sender</span></tt> and
<tt class="docutils literal"><span class="pre">cc_myself</span></tt>. <a class="reference internal" href="../../ref/forms/fields.html#django.forms.CharField" title="django.forms.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a>, <a class="reference internal" href="../../ref/forms/fields.html#django.forms.EmailField" title="django.forms.EmailField"><tt class="xref py py-class docutils literal"><span class="pre">EmailField</span></tt></a> and
<a class="reference internal" href="../../ref/forms/fields.html#django.forms.BooleanField" title="django.forms.BooleanField"><tt class="xref py py-class docutils literal"><span class="pre">BooleanField</span></tt></a> are just three of the available field types; a full list
can be found in <a class="reference internal" href="../../ref/forms/fields.html"><em>Form fields</em></a>.</p>
<div class="section" id="s-widgets">
<span id="widgets"></span><h4>Widgets<a class="headerlink" href="#widgets" title="Permalink to this headline">¶</a></h4>
<p>Each form field has a corresponding <a class="reference internal" href="../../ref/forms/widgets.html"><em>Widget class</em></a>,
which in turn corresponds to an HTML form widget such as <tt class="docutils literal"><span class="pre">&lt;input</span>
<span class="pre">type=&quot;text&quot;&gt;</span></tt>.</p>
<p>In most cases, the field will have a sensible default widget. For example, by
default, a <a class="reference internal" href="../../ref/forms/fields.html#django.forms.CharField" title="django.forms.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> will have a <a class="reference internal" href="../../ref/forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a> widget, that
produces an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> in the HTML. If you needed <tt class="docutils literal"><span class="pre">&lt;textarea&gt;</span></tt>
instead, you&#8217;d specify the appropriate widget when defining your form field,
as we have done for the <tt class="docutils literal"><span class="pre">message</span></tt> field.</p>
</div>
<div class="section" id="s-field-data">
<span id="field-data"></span><h4>Field data<a class="headerlink" href="#field-data" title="Permalink to this headline">¶</a></h4>
<p>Whatever the data submitted with a form, once it has been successfully
validated by calling <tt class="docutils literal"><span class="pre">is_valid()</span></tt> (and <tt class="docutils literal"><span class="pre">is_valid()</span></tt> has returned <tt class="docutils literal"><span class="pre">True</span></tt>),
the validated form data will be in the <tt class="docutils literal"><span class="pre">form.cleaned_data</span></tt> dictionary. This
data will have been nicely converted into Python types for you.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You can still access the unvalidated data directly from <tt class="docutils literal"><span class="pre">request.POST</span></tt> at
this point, but the validated data is better.</p>
</div>
<p>In the contact form example above, <tt class="docutils literal"><span class="pre">cc_myself</span></tt> will be a boolean value.
Likewise, fields such as <a class="reference internal" href="../../ref/forms/fields.html#django.forms.IntegerField" title="django.forms.IntegerField"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a> and <a class="reference internal" href="../../ref/forms/fields.html#django.forms.FloatField" title="django.forms.FloatField"><tt class="xref py py-class docutils literal"><span class="pre">FloatField</span></tt></a> convert
values to a Python <tt class="docutils literal"><span class="pre">int</span></tt> and <tt class="docutils literal"><span class="pre">float</span></tt> respectively.</p>
<p>Here&#8217;s how the form data could be processed in the view that handles this form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.core.mail</span> <span class="kn">import</span> <span class="n">send_mail</span>

<span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
    <span class="n">subject</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;subject&#39;</span><span class="p">]</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;message&#39;</span><span class="p">]</span>
    <span class="n">sender</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;sender&#39;</span><span class="p">]</span>
    <span class="n">cc_myself</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;cc_myself&#39;</span><span class="p">]</span>

    <span class="n">recipients</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;info@example.com&#39;</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">cc_myself</span><span class="p">:</span>
        <span class="n">recipients</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sender</span><span class="p">)</span>

    <span class="n">send_mail</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">recipients</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s">&#39;/thanks/&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">For more on sending email from Django, see <a class="reference internal" href="../email.html"><em>Sending email</em></a>.</p>
</div>
<p>Some field types need some extra handling. For example, files that are uploaded
using a form need to be handled differently (they can be retrieved from
<tt class="docutils literal"><span class="pre">request.FILES</span></tt>, rather than <tt class="docutils literal"><span class="pre">request.POST</span></tt>). For details of how to handle
file uploads with your form, see <a class="reference internal" href="../../ref/forms/api.html#binding-uploaded-files"><em>Binding uploaded files to a form</em></a>.</p>
</div>
</div>
</div>
<div class="section" id="s-working-with-form-templates">
<span id="working-with-form-templates"></span><h2>Working with form templates<a class="headerlink" href="#working-with-form-templates" title="Permalink to this headline">¶</a></h2>
<p>All you need to do to get your form into a template is to place the form
instance into the template context. So if your form is called <tt class="docutils literal"><span class="pre">form</span></tt> in the
context, <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form</span> <span class="pre">}}</span></tt> will render its <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> and <tt class="docutils literal"><span class="pre">&lt;input&gt;</span></tt> elements
appropriately.</p>
<div class="section" id="s-form-rendering-options">
<span id="form-rendering-options"></span><h3>Form rendering options<a class="headerlink" href="#form-rendering-options" title="Permalink to this headline">¶</a></h3>
<div class="admonition-additional-form-template-furniture admonition">
<p class="first admonition-title">Additional form template furniture</p>
<p class="last">Don&#8217;t forget that a form&#8217;s output does <em>not</em> include the surrounding
<tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt> tags, or the form&#8217;s <tt class="docutils literal"><span class="pre">submit</span></tt> control. You will have to provide
these yourself.</p>
</div>
<p>There are other output options though for the <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt>/<tt class="docutils literal"><span class="pre">&lt;input&gt;</span></tt> pairs:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.as_table</span> <span class="pre">}}</span></tt> will render them as table cells wrapped in <tt class="docutils literal"><span class="pre">&lt;tr&gt;</span></tt>
tags</li>
<li><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.as_p</span> <span class="pre">}}</span></tt> will render them wrapped in <tt class="docutils literal"><span class="pre">&lt;p&gt;</span></tt> tags</li>
<li><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.as_ul</span> <span class="pre">}}</span></tt> will render them wrapped in <tt class="docutils literal"><span class="pre">&lt;li&gt;</span></tt> tags</li>
</ul>
<p>Note that you&#8217;ll have to provide the surrounding <tt class="docutils literal"><span class="pre">&lt;table&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&lt;ul&gt;</span></tt>
elements yourself.</p>
<p>Here&#8217;s the output of <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.as_p</span> <span class="pre">}}</span></tt> for our <tt class="docutils literal"><span class="pre">ContactForm</span></tt> instance:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;p&gt;&lt;label</span> <span class="na">for=</span><span class="s">&quot;id_subject&quot;</span><span class="nt">&gt;</span>Subject:<span class="nt">&lt;/label&gt;</span>
    <span class="nt">&lt;input</span> <span class="na">id=</span><span class="s">&quot;id_subject&quot;</span> <span class="na">type=</span><span class="s">&quot;text&quot;</span> <span class="na">name=</span><span class="s">&quot;subject&quot;</span> <span class="na">maxlength=</span><span class="s">&quot;100&quot;</span> <span class="nt">/&gt;&lt;/p&gt;</span>
<span class="nt">&lt;p&gt;&lt;label</span> <span class="na">for=</span><span class="s">&quot;id_message&quot;</span><span class="nt">&gt;</span>Message:<span class="nt">&lt;/label&gt;</span>
    <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">&quot;text&quot;</span> <span class="na">name=</span><span class="s">&quot;message&quot;</span> <span class="na">id=</span><span class="s">&quot;id_message&quot;</span> <span class="nt">/&gt;&lt;/p&gt;</span>
<span class="nt">&lt;p&gt;&lt;label</span> <span class="na">for=</span><span class="s">&quot;id_sender&quot;</span><span class="nt">&gt;</span>Sender:<span class="nt">&lt;/label&gt;</span>
    <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">&quot;email&quot;</span> <span class="na">name=</span><span class="s">&quot;sender&quot;</span> <span class="na">id=</span><span class="s">&quot;id_sender&quot;</span> <span class="nt">/&gt;&lt;/p&gt;</span>
<span class="nt">&lt;p&gt;&lt;label</span> <span class="na">for=</span><span class="s">&quot;id_cc_myself&quot;</span><span class="nt">&gt;</span>Cc myself:<span class="nt">&lt;/label&gt;</span>
    <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">&quot;checkbox&quot;</span> <span class="na">name=</span><span class="s">&quot;cc_myself&quot;</span> <span class="na">id=</span><span class="s">&quot;id_cc_myself&quot;</span> <span class="nt">/&gt;&lt;/p&gt;</span>
</pre></div>
</div>
<p>Note that each form field has an ID attribute set to <tt class="docutils literal"><span class="pre">id_&lt;field-name&gt;</span></tt>, which
is referenced by the accompanying label tag. This is important in ensuring that
forms are accessible to assistive technology such as screen reader software.
You can also <a class="reference internal" href="../../ref/forms/api.html#ref-forms-api-configuring-label"><em>customize the way in which labels and ids are generated</em></a>.</p>
<p>See <a class="reference internal" href="../../ref/forms/api.html#ref-forms-api-outputting-html"><em>Outputting forms as HTML</em></a> for more on this.</p>
</div>
<div class="section" id="s-rendering-fields-manually">
<span id="rendering-fields-manually"></span><h3>Rendering fields manually<a class="headerlink" href="#rendering-fields-manually" title="Permalink to this headline">¶</a></h3>
<p>We don&#8217;t have to let Django unpack the form&#8217;s fields; we can do it manually if
we like (allowing us to reorder the fields, for example). Each field is
available as an attribute of the form using <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.name_of_field</span> <span class="pre">}}</span></tt>, and
in a Django template, will be rendered appropriately. For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">form.non_field_errors</span> <span class="cp">}}</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;fieldWrapper&quot;</span><span class="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.subject.errors</span> <span class="cp">}}</span>
    <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">form.subject.id_for_label</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="nt">&gt;</span>Email subject:<span class="nt">&lt;/label&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.subject</span> <span class="cp">}}</span>
<span class="nt">&lt;/div&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;fieldWrapper&quot;</span><span class="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.message.errors</span> <span class="cp">}}</span>
    <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">form.message.id_for_label</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="nt">&gt;</span>Your message:<span class="nt">&lt;/label&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.message</span> <span class="cp">}}</span>
<span class="nt">&lt;/div&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;fieldWrapper&quot;</span><span class="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.sender.errors</span> <span class="cp">}}</span>
    <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">form.sender.id_for_label</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="nt">&gt;</span>Your email address:<span class="nt">&lt;/label&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.sender</span> <span class="cp">}}</span>
<span class="nt">&lt;/div&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;fieldWrapper&quot;</span><span class="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.cc_myself.errors</span> <span class="cp">}}</span>
    <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">form.cc_myself.id_for_label</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="nt">&gt;</span>CC yourself?<span class="nt">&lt;/label&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.cc_myself</span> <span class="cp">}}</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>
</div>
<p>Complete <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> element can also be generated using the
<a class="reference internal" href="../../ref/forms/api.html#django.forms.BoundField.label_tag" title="django.forms.BoundField.label_tag"><tt class="xref py py-meth docutils literal"><span class="pre">label_tag()</span></tt></a>. For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;fieldWrapper&quot;</span><span class="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">form.subject.errors</span> <span class="cp">}}</span>
    <span class="cp">{{</span> <span class="nv">form.subject.label_tag</span> <span class="cp">}}</span>
    <span class="cp">{{</span> <span class="nv">form.subject</span> <span class="cp">}}</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>
</div>
<div class="section" id="s-rendering-form-error-messages">
<span id="rendering-form-error-messages"></span><h4>Rendering form error messages<a class="headerlink" href="#rendering-form-error-messages" title="Permalink to this headline">¶</a></h4>
<p>Of course, the price of this flexibility is more work. Until now we haven&#8217;t had
to worry about how to display form errors, because that&#8217;s taken care of for us.
In this example we have had to make sure we take care of any errors for each
field and any errors for the form as a whole. Note <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.non_field_errors</span>
<span class="pre">}}</span></tt> at the top of the form and the template lookup for errors on each field.</p>
<p>Using <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.name_of_field.errors</span> <span class="pre">}}</span></tt> displays a list of form errors,
rendered as an unordered list. This might look like:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;ul</span> <span class="na">class=</span><span class="s">&quot;errorlist&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;li&gt;</span>Sender is required.<span class="nt">&lt;/li&gt;</span>
<span class="nt">&lt;/ul&gt;</span>
</pre></div>
</div>
<p>The list has a CSS class of <tt class="docutils literal"><span class="pre">errorlist</span></tt> to allow you to style its appearance.
If you wish to further customize the display of errors you can do so by looping
over them:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">form.subject.errors</span> <span class="cp">%}</span>
    <span class="nt">&lt;ol&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">error</span> <span class="k">in</span> <span class="nv">form.subject.errors</span> <span class="cp">%}</span>
        <span class="nt">&lt;li&gt;&lt;strong&gt;</span><span class="cp">{{</span> <span class="nv">error</span><span class="o">|</span><span class="nf">escape</span> <span class="cp">}}</span><span class="nt">&lt;/strong&gt;&lt;/li&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="nt">&lt;/ol&gt;</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="../../ref/forms/api.html"><em>The Forms API</em></a> for more on errors, styling, and working with form
attributes in templates.</p>
</div>
</div>
<div class="section" id="s-looping-over-the-form-s-fields">
<span id="looping-over-the-form-s-fields"></span><h3>Looping over the form&#8217;s fields<a class="headerlink" href="#looping-over-the-form-s-fields" title="Permalink to this headline">¶</a></h3>
<p>If you&#8217;re using the same HTML for each of your form fields, you can reduce
duplicate code by looping through each field in turn using a <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">%}</span></tt>
loop:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">for</span> <span class="nv">field</span> <span class="k">in</span> <span class="nv">form</span> <span class="cp">%}</span>
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;fieldWrapper&quot;</span><span class="nt">&gt;</span>
        <span class="cp">{{</span> <span class="nv">field.errors</span> <span class="cp">}}</span>
        <span class="cp">{{</span> <span class="nv">field.label_tag</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">field</span> <span class="cp">}}</span>
    <span class="nt">&lt;/div&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Useful attributes on <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field</span> <span class="pre">}}</span></tt> include:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.label</span> <span class="pre">}}</span></tt></dt>
<dd>The label of the field, e.g. <tt class="docutils literal"><span class="pre">Email</span> <span class="pre">address</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.label_tag</span> <span class="pre">}}</span></tt></dt>
<dd><p class="first">The field&#8217;s label wrapped in the appropriate HTML <tt class="docutils literal"><span class="pre">&lt;label&gt;</span></tt> tag.</p>
<div class="last versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>This includes the form&#8217;s <a class="reference internal" href="../../ref/forms/api.html#django.forms.Form.label_suffix" title="django.forms.Form.label_suffix"><tt class="xref py py-attr docutils literal"><span class="pre">label_suffix</span></tt></a>. For
example, the default <tt class="docutils literal"><span class="pre">label_suffix</span></tt> is a colon:</p>
<div class="highlight-python"><div class="highlight"><pre>&lt;label for=&quot;id_email&quot;&gt;Email address:&lt;/label&gt;
</pre></div>
</div>
</div>
</dd>
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.id_for_label</span> <span class="pre">}}</span></tt></dt>
<dd>The ID that will be used for this field (<tt class="docutils literal"><span class="pre">id_email</span></tt> in the example
above). If you are constructing the label manually, you may want to use
this in lieu of <tt class="docutils literal"><span class="pre">label_tag</span></tt>. It&#8217;s also useful, for example, if you have
some inline JavaScript and want to avoid hardcoding the field&#8217;s ID.</dd>
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.value</span> <span class="pre">}}</span></tt></dt>
<dd>The value of the field. e.g <tt class="docutils literal"><span class="pre">someone&#64;example.com</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.html_name</span> <span class="pre">}}</span></tt></dt>
<dd>The name of the field that will be used in the input element&#8217;s name
field. This takes the form prefix into account, if it has been set.</dd>
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.help_text</span> <span class="pre">}}</span></tt></dt>
<dd>Any help text that has been associated with the field.</dd>
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.errors</span> <span class="pre">}}</span></tt></dt>
<dd>Outputs a <tt class="docutils literal"><span class="pre">&lt;ul</span> <span class="pre">class=&quot;errorlist&quot;&gt;</span></tt> containing any validation errors
corresponding to this field. You can customize the presentation of
the errors with a <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">error</span> <span class="pre">in</span> <span class="pre">field.errors</span> <span class="pre">%}</span></tt> loop. In this
case, each object in the loop is a simple string containing the error
message.</dd>
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.is_hidden</span> <span class="pre">}}</span></tt></dt>
<dd>This attribute is <tt class="docutils literal"><span class="pre">True</span></tt> if the form field is a hidden field and
<tt class="docutils literal"><span class="pre">False</span></tt> otherwise. It&#8217;s not particularly useful as a template
variable, but could be useful in conditional tests such as:</dd>
</dl>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">field.is_hidden</span> <span class="cp">%}</span>
   <span class="c">{# Do something special #}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">field.field</span> <span class="pre">}}</span></tt></dt>
<dd>The <a class="reference internal" href="../../ref/forms/fields.html#django.forms.Field" title="django.forms.Field"><tt class="xref py py-class docutils literal"><span class="pre">Field</span></tt></a> instance from the form class that
this <a class="reference internal" href="../../ref/forms/api.html#django.forms.BoundField" title="django.forms.BoundField"><tt class="xref py py-class docutils literal"><span class="pre">BoundField</span></tt></a> wraps. You can use it to access
<a class="reference internal" href="../../ref/forms/fields.html#django.forms.Field" title="django.forms.Field"><tt class="xref py py-class docutils literal"><span class="pre">Field</span></tt></a> attributes, e.g.
<tt class="docutils literal"><span class="pre">{{</span> <span class="pre">char_field.field.max_length</span> <span class="pre">}}</span></tt>.</dd>
</dl>
<div class="section" id="s-looping-over-hidden-and-visible-fields">
<span id="looping-over-hidden-and-visible-fields"></span><h4>Looping over hidden and visible fields<a class="headerlink" href="#looping-over-hidden-and-visible-fields" title="Permalink to this headline">¶</a></h4>
<p>If you&#8217;re manually laying out a form in a template, as opposed to relying on
Django&#8217;s default form layout, you might want to treat <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;hidden&quot;&gt;</span></tt>
fields differently from non-hidden fields. For example, because hidden fields
don&#8217;t display anything, putting error messages &#8220;next to&#8221; the field could cause
confusion for your users &#8211; so errors for those fields should be handled
differently.</p>
<p>Django provides two methods on a form that allow you to loop over the hidden
and visible fields independently: <tt class="docutils literal"><span class="pre">hidden_fields()</span></tt> and
<tt class="docutils literal"><span class="pre">visible_fields()</span></tt>. Here&#8217;s a modification of an earlier example that uses
these two methods:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="c">{# Include the hidden fields #}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">hidden</span> <span class="k">in</span> <span class="nv">form.hidden_fields</span> <span class="cp">%}</span>
<span class="cp">{{</span> <span class="nv">hidden</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="c">{# Include the visible fields #}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">field</span> <span class="k">in</span> <span class="nv">form.visible_fields</span> <span class="cp">%}</span>
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;fieldWrapper&quot;</span><span class="nt">&gt;</span>
        <span class="cp">{{</span> <span class="nv">field.errors</span> <span class="cp">}}</span>
        <span class="cp">{{</span> <span class="nv">field.label_tag</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">field</span> <span class="cp">}}</span>
    <span class="nt">&lt;/div&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This example does not handle any errors in the hidden fields. Usually, an
error in a hidden field is a sign of form tampering, since normal form
interaction won&#8217;t alter them. However, you could easily insert some error
displays for those form errors, as well.</p>
</div>
</div>
<div class="section" id="s-reusable-form-templates">
<span id="reusable-form-templates"></span><h3>Reusable form templates<a class="headerlink" href="#reusable-form-templates" title="Permalink to this headline">¶</a></h3>
<p>If your site uses the same rendering logic for forms in multiple places, you
can reduce duplication by saving the form&#8217;s loop in a standalone template and
using the <a class="reference internal" href="../../ref/templates/builtins.html#std:templatetag-include"><tt class="xref std std-ttag docutils literal"><span class="pre">include</span></tt></a> tag to reuse it in other templates:</p>
<div class="highlight-html+django"><div class="highlight"><pre># In your form template:
<span class="cp">{%</span> <span class="k">include</span> <span class="s2">&quot;form_snippet.html&quot;</span> <span class="cp">%}</span>

# In form_snippet.html:
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">field</span> <span class="k">in</span> <span class="nv">form</span> <span class="cp">%}</span>
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;fieldWrapper&quot;</span><span class="nt">&gt;</span>
        <span class="cp">{{</span> <span class="nv">field.errors</span> <span class="cp">}}</span>
        <span class="cp">{{</span> <span class="nv">field.label_tag</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">field</span> <span class="cp">}}</span>
    <span class="nt">&lt;/div&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>If the form object passed to a template has a different name within the
context, you can alias it using the <tt class="docutils literal"><span class="pre">with</span></tt> argument of the <a class="reference internal" href="../../ref/templates/builtins.html#std:templatetag-include"><tt class="xref std std-ttag docutils literal"><span class="pre">include</span></tt></a>
tag:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">include</span> <span class="s2">&quot;form_snippet.html&quot;</span> <span class="k">with</span> <span class="nv">form</span><span class="o">=</span><span class="nv">comment_form</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>If you find yourself doing this often, you might consider creating a custom
<a class="reference internal" href="../../howto/custom-template-tags.html#howto-custom-template-tags-inclusion-tags"><em>inclusion tag</em></a>.</p>
</div>
</div>
<div class="section" id="s-further-topics">
<span id="further-topics"></span><h2>Further topics<a class="headerlink" href="#further-topics" title="Permalink to this headline">¶</a></h2>
<p>This covers the basics, but forms can do a whole lot more:</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="formsets.html">Formsets</a><ul>
<li class="toctree-l2"><a class="reference internal" href="formsets.html#using-initial-data-with-a-formset">Using initial data with a formset</a></li>
<li class="toctree-l2"><a class="reference internal" href="formsets.html#limiting-the-maximum-number-of-forms">Limiting the maximum number of forms</a></li>
<li class="toctree-l2"><a class="reference internal" href="formsets.html#formset-validation">Formset validation</a></li>
<li class="toctree-l2"><a class="reference internal" href="formsets.html#validating-the-number-of-forms-in-a-formset">Validating the number of forms in a formset</a></li>
<li class="toctree-l2"><a class="reference internal" href="formsets.html#dealing-with-ordering-and-deletion-of-forms">Dealing with ordering and deletion of forms</a></li>
<li class="toctree-l2"><a class="reference internal" href="formsets.html#adding-additional-fields-to-a-formset">Adding additional fields to a formset</a></li>
<li class="toctree-l2"><a class="reference internal" href="formsets.html#using-a-formset-in-views-and-templates">Using a formset in views and templates</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="modelforms.html">Creating forms from models</a><ul>
<li class="toctree-l2"><a class="reference internal" href="modelforms.html#modelform"><tt class="docutils literal"><span class="pre">ModelForm</span></tt></a></li>
<li class="toctree-l2"><a class="reference internal" href="modelforms.html#model-formsets">Model formsets</a></li>
<li class="toctree-l2"><a class="reference internal" href="modelforms.html#inline-formsets">Inline formsets</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="media.html">Form Assets (the <tt class="docutils literal"><span class="pre">Media</span></tt> class)</a><ul>
<li class="toctree-l2"><a class="reference internal" href="media.html#assets-as-a-static-definition">Assets as a static definition</a></li>
<li class="toctree-l2"><a class="reference internal" href="media.html#media-as-a-dynamic-property"><tt class="docutils literal"><span class="pre">Media</span></tt> as a dynamic property</a></li>
<li class="toctree-l2"><a class="reference internal" href="media.html#paths-in-asset-definitions">Paths in asset definitions</a></li>
<li class="toctree-l2"><a class="reference internal" href="media.html#media-objects"><tt class="docutils literal"><span class="pre">Media</span></tt> objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="media.html#media-on-forms"><tt class="docutils literal"><span class="pre">Media</span></tt> on Forms</a></li>
</ul>
</li>
</ul>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference internal" href="../../ref/forms/index.html"><em>The Forms Reference</em></a></dt>
<dd>Covers the full API reference, including form fields, form widgets,
and form and field validation.</dd>
</dl>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Working with forms</a><ul>
<li><a class="reference internal" href="#html-forms">HTML forms</a><ul>
<li><a class="reference internal" href="#get-and-post"><tt class="docutils literal"><span class="pre">GET</span></tt> and <tt class="docutils literal"><span class="pre">POST</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#django-s-role-in-forms">Django&#8217;s role in forms</a></li>
<li><a class="reference internal" href="#forms-in-django">Forms in Django</a><ul>
<li><a class="reference internal" href="#the-django-form-class">The Django <tt class="docutils literal"><span class="pre">Form</span></tt> class</a></li>
<li><a class="reference internal" href="#instantiating-processing-and-rendering-forms">Instantiating, processing, and rendering forms</a></li>
</ul>
</li>
<li><a class="reference internal" href="#building-a-form">Building a form</a><ul>
<li><a class="reference internal" href="#the-work-that-needs-to-be-done">The work that needs to be done</a></li>
<li><a class="reference internal" href="#building-a-form-in-django">Building a form in Django</a><ul>
<li><a class="reference internal" href="#the-form-class">The <tt class="docutils literal"><span class="pre">Form</span></tt> class</a></li>
<li><a class="reference internal" href="#the-view">The view</a></li>
<li><a class="reference internal" href="#the-template">The template</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#more-about-django-form-classes">More about Django <tt class="docutils literal"><span class="pre">Form</span></tt> classes</a><ul>
<li><a class="reference internal" href="#bound-and-unbound-form-instances">Bound and unbound form instances</a></li>
<li><a class="reference internal" href="#more-on-fields">More on fields</a><ul>
<li><a class="reference internal" href="#widgets">Widgets</a></li>
<li><a class="reference internal" href="#field-data">Field data</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#working-with-form-templates">Working with form templates</a><ul>
<li><a class="reference internal" href="#form-rendering-options">Form rendering options</a></li>
<li><a class="reference internal" href="#rendering-fields-manually">Rendering fields manually</a><ul>
<li><a class="reference internal" href="#rendering-form-error-messages">Rendering form error messages</a></li>
</ul>
</li>
<li><a class="reference internal" href="#looping-over-the-form-s-fields">Looping over the form&#8217;s fields</a><ul>
<li><a class="reference internal" href="#looping-over-hidden-and-visible-fields">Looping over hidden and visible fields</a></li>
</ul>
</li>
<li><a class="reference internal" href="#reusable-form-templates">Reusable form templates</a></li>
</ul>
</li>
<li><a class="reference internal" href="#further-topics">Further topics</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="../http/sessions.html">How to use sessions</a></li>
    
    
      <li>Next: <a href="formsets.html">Formsets</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
        <ul><li>Working with forms</li></ul>
        </li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/topics/forms/index.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="../http/sessions.html" title="How to use sessions">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="formsets.html" title="Formsets">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>